<?php
	
	//写日志
	function writeFile($file,$str,$mode='a') {  
		$fp = fopen($file,$mode); 
		$encode = check_utf8($str); 
		if($encode){ 
			$str=iconv('UTF-8','GBK',$str);
		} 
		$startTime = microtime();
 		do{
        	$canWrite = flock($fp, LOCK_EX);
  			if(!$canWrite) usleep(round(rand(0, 100)*1000));
 		} while ((!$canWrite)&&((microtime()-$startTime) < 1000));
		if(!$fp) { 
			Return false; 
		} else { 
			fwrite($fp,"$str\r\n"); 
			fclose($fp);
			Return true; 
		} 
	} 
	//检测是否是utf8
	function check_utf8($str){  
	    $len = strlen($str);  
	    for($i = 0; $i < $len; $i++){  
	        $c = ord($str[$i]);  
	        if ($c > 128) {  
	            if (($c > 247)) return false;  
	            elseif ($c > 239) $bytes = 4;  
	            elseif ($c > 223) $bytes = 3;  
	            elseif ($c > 191) $bytes = 2;  
	            else return false;  
	            if (($i + $bytes) > $len) return false;  
	            while ($bytes > 1) {  
	                $i++;  
	                $b = ord($str[$i]);  
	                if ($b < 128 || $b > 191) return false;  
	                $bytes--;  
	        	}  
	    	}  
    	}  
	    return true;  
	} // end of check_utf8
		
	//转义
	function _addslashes($arr) {
    	foreach($arr as $k=>$v) {
	        if(is_string($v)) {
	            $arr[$k] = addslashes($v);
	        } else if(is_array($v)) {  // 再加判断,如果是数组,调用自身,再转
	            $arr[$k] = _addslashes($v);
	        }
    	}
    
    	return $arr;
	}

	//html跳转
	function htmlRedirect($url,$arr){

		$html='<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head>	
		<body onLoad="document.redirect.submit();"><form name="redirect" action="'.$url.'"'.' method="post">';
		foreach($arr as $k=>$v){
			$html.=	'<input type="hidden" name="'.$k.'" value="'.$v.'"'.'/>';
		}
		$html.='</form>';
		$html.='</body>';
		$html.='</html>';
		return $html;
	}

	function receiveXml($date){
		$xml= "<result>";
		foreach($date as $k=>$v){
			$xml.='<'.$k.'>'.$v.'</'.$k.'>';
		}
		if(!$date['md5check']){
			$xml.='<md5check></md5check>';
		}
		$xml.="</result>";
		return $xml;
	}

	function curl($url,$date=null){
		$ch=curl_init();
		curl_setopt($ch, CURLOPT_URL,$url);
		if(!empty($date)){
			curl_setopt($ch, CURLOPT_POST,"1");
			curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($date));
		}
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查 
		//curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true); // 从证书中检查SSL加密算法是否存在
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); //默认是 0 直接返回获取的输出的文本流
		$result = curl_exec($ch);
		//var_dump($result);
		curl_close($ch);
		return $result;
	}

	

//根据mysqli获取的资源获取数组
function getALL($result){
	$list = array();
	while($row =$result->fetch_array()){                        //循环输出结果集中的记录
	    $list[] = $row;
	}
	return $list;
}

//获取ip
function getIP(){
	global $ip;
	
	if (getenv("HTTP_CLIENT_IP"))
		$ip = getenv("HTTP_CLIENT_IP");
	else if(getenv("HTTP_X_FORWARDED_FOR"))
		$ip = getenv("HTTP_X_FORWARDED_FOR");
	else if(getenv("REMOTE_ADDR"))
		$ip = getenv("REMOTE_ADDR");
	else 
		$ip = "Unknow";
		
	return $ip;
}
/**
 * 发送post请求
 * @param string $url 请求地址
 * @param array $post_data post键值对数据
 * @return string
 */
function send_post($url, $post_data) {
    //$postdata = http_build_query($post_data);
    $options = array(
        'http' => array(
            'method' => 'POST',
            'header' => 'Content-type:application/x-www-form-urlencoded',
            'content' => '',
            'timeout' => 15 * 60 // 超时时间（单位:s）
        )
    );
    $context = stream_context_create($options);
    $result = file_get_contents($url, false, $context);
    return $result;
}
/**
 * 模拟post进行url请求
 * @param string $url
 * @param string $param
 */
function request_post($url = '', $param = '') {
    if (empty($url) || empty($param)) {
        return false;
    }

    $postUrl = $url;
    $curlPost = $param;
    $ch = curl_init();//初始化curl
    curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
    curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
    curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
    curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
    $data = curl_exec($ch);//运行curl
    curl_close($ch);

    return $data;
}

?>